//
//  MYXMoneyComesViewController.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/11/4.
//  Copyright © 2017年 hbjApple. All rights reserved.
//钱来钱往

#import "MYXMoneyComesViewController.h"
#import "MYXMoneyComesCell.h"
#import "MYXMoneyComesTopView.h"
#import "MYCmymoneylogViewModel.h"
#import "MYCmymoneylogModel.h"

static NSString *const MYXMoneyComesCellIdentifier = @"MYXMoneyComesCell";
static NSString *const MYXMoneyComesTopViewIdentifier = @"MYXMoneyComesTopView";

@interface MYXMoneyComesViewController ()<UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIAlertViewDelegate,UIAlertViewDelegate>{
    MYCmymoneylogViewModel *MYCmymoneylogViewM;
}
@property(nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) MYXMoneyComesTopView *MYXMoneyComesTopVi;
@property(nonatomic, assign) int pageStart;
@property(nonatomic, assign) int type;//判断是否刷新
@end

@implementation MYXMoneyComesViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageStart=1;
    if (MYCmymoneylogViewM == nil) {
        MYCmymoneylogViewM = [[MYCmymoneylogViewModel alloc] init];
    }
    
    self.tableView.backgroundColor = [UIColor colorWithRed:0.92f green:0.92f blue:0.94f alpha:1.00f];
    [self.view addSubview:self.tableView];
    [self.tableView.mj_header beginRefreshing];
    [self.tableView registerNib:[UINib nibWithNibName:MYXMoneyComesCellIdentifier bundle:nil] forCellReuseIdentifier:MYXMoneyComesCellIdentifier];
    
}
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-SafeAreaTopHeight) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor colorWithRed:0.94f green:0.94f blue:0.96f alpha:1.00f];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        WS(weakSelf)
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf loadNewData];
            
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [weakSelf loadMorewData];
        }];
    }
    return _tableView;
}
#pragma mark - request

- (void)loadNewData
{
    [self.tableView.mj_header beginRefreshing];
    self.pageStart = 1;
    self.type =0;
    [self Cmymoneylog:self.pageStart];
    
}
- (void)loadMorewData
{
    if (self.tableView.mj_header.isRefreshing) {
        return;
    }
    self.pageStart ++;
    self.type =1;
    [self Cmymoneylog:self.pageStart];
}

#pragma mark - tableview datasource and delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return MYCmymoneylogViewM.CmymoneylogArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MYXMoneyComesCell *MYXMoneyComesCe = (MYXMoneyComesCell *)[tableView dequeueReusableCellWithIdentifier:MYXMoneyComesCellIdentifier];
    MYXMoneyComesCe.selectionStyle = UITableViewCellSelectionStyleNone;
    MYCmymoneylogModel *MYCmymoneylogMo=MYCmymoneylogViewM.CmymoneylogArray[indexPath.section];
    MYXMoneyComesCe.MYCmymoneylogM =MYCmymoneylogMo;
    return MYXMoneyComesCe;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    return 153;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView =[[UIView alloc]init];
    headView.backgroundColor =[UIColor colorWithRed:0.95f green:0.95f blue:0.95f alpha:1.00f];
    headView.frame = CGRectMake(0, 0, WIDTH, 20);
    
    
    self.MYXMoneyComesTopVi                                =[[NSBundle mainBundle]loadNibNamed:MYXMoneyComesTopViewIdentifier owner:self options:nil][0];
    self.MYXMoneyComesTopVi.frame = CGRectMake(0, 0, WIDTH,headView.frame.size.height);
     MYCmymoneylogModel *MYCmymoneylogMo=MYCmymoneylogViewM.CmymoneylogArray[section];
    self.MYXMoneyComesTopVi.timeLabel.text =[MYXDateTimeHelper timeWithTimeIntervalString:MYCmymoneylogMo.change_time];
    [headView addSubview:self.MYXMoneyComesTopVi];
    return headView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *FooterView =[[UIView alloc]init];
    FooterView.frame = CGRectMake(0, 0, WIDTH, 0.1);
    return FooterView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

#pragma mark -  钱来钱往接口


-(void)Cmymoneylog:(int)page{
    if (self.type==0) {
        [MYCmymoneylogViewM.CmymoneylogArray removeAllObjects];
    }
    [MYCmymoneylogViewM  loadCmymoneylog:page WithResult:^(BOOL isSucess) {
        if (isSucess) {
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }else{
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
       
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
